package com.example.rwq_test.ThreadPool;

import java.util.concurrent.*;

/**
 * TODO note
 *
 * @author renweiqiang
 * @version 1.0.0
 * @createTime 2024/4/11
 */
public class MyThreadFactory implements ThreadFactory {

    private int threadIndex = 0;

    @Override
    public Thread newThread(Runnable r) {
        Thread result = new Thread(r);
        result.setName("my" + "-pool-" + this.threadIndex);
        ++this.threadIndex;
        return result;
    }


    public static void main(String[] args) throws InterruptedException {

        ThreadPoolExecutor executorPool = new ThreadPoolExecutor(3, 5, 10,
                TimeUnit.SECONDS, new LinkedBlockingQueue<>(7), new MyThreadFactory() , new ThreadPoolExecutor.AbortPolicy());

        for (int i = 0; i < 50; i++) {
            int finalI = i;
            executorPool.execute(() -> {
                System.out.println(finalI);
                System.out.println("currentThreadName: " + Thread.currentThread().getName());
            });
        }
    }
}
